package com.qiwx.pigeon.fragment.produce;

import android.content.Intent;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andview.refreshview.XRefreshView;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DBSqlUtil;
import com.qiwx.pigeon.Utils.LogUtil;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.activity.MainActivity;
import com.qiwx.pigeon.activity.produce.AddLayEggDateProduceActivity;
import com.qiwx.pigeon.adapter.produce.CheckKWAdapter;
import com.qiwx.pigeon.base.BaseMVPFragment;
import com.qiwx.pigeon.base.Constant;
import com.qiwx.pigeon.data.CageStatusConfig;
import com.qiwx.pigeon.data.params.CageParam;
import com.qiwx.pigeon.data.params.FertilizeParam;
import com.qiwx.pigeon.data.vo.CageBeanVo;
import com.qiwx.pigeon.data.vo.CageStatusRecordBean;
import com.qiwx.pigeon.data.vo.FertilizeBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.fragment.ProduceFragment;
import com.qiwx.pigeon.helper.DialogHelper;
import com.qiwx.pigeon.presenter.CagePresenter;
import com.qiwx.pigeon.presenter.OperatePresenter;
import com.qiwx.pigeon.viewinterface.ICageView;
import com.qiwx.pigeon.viewinterface.IOperateView;
import com.qiwx.pigeon.widget.itemderection.CustomGridItemDecoration;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @description
 * @Author qiwx
 * @Date 2020-03-29 16:52
 * 查看空窝
 **/
public class CheckKWOperationFragment extends BaseMVPFragment<ICageView, CagePresenter> implements ICageView, IOperateView {

    int produceStatus;//生产状态
    List<CageBeanVo> data = new ArrayList<>();
    CheckKWAdapter adapter;
    @BindView(R.id.txt_nodata)
    TextView txtNodata;
    @BindView(R.id.lin_nodata)
    LinearLayout linNodata;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.xRefreshView)
    XRefreshView xRefreshView;
    @BindView(R.id.rel_nodata_recycleview)
    RelativeLayout relNodataRecycleview;
    int pageSize = 20;//每页显示10条记录
    CageParam cageParam;
    boolean isFresh = true;
    OperatePresenter operatePresenter;
    public int selectPos = -1;
    boolean isEmpty = true;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_check_kw;
    }

    @Override
    public void initAfterOnCreate() {
        super.initAfterOnCreate();
        cageParam = new CageParam();
        cageParam.setPageSize(pageSize);
        cageParam.setPageNum(1);
        cageParam.setProduce_status(CageStatusConfig.PRODUCE_KW);
        cageParam.setStatus(CageStatusConfig.STATUS_YGYM);
        initView();
        operatePresenter = new OperatePresenter(new WeakReference(this));
        loadData();
    }

    @Override
    public CagePresenter createPresenter() {
        return new CagePresenter(new WeakReference<>(this));
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onReFreshData() {
        super.onReFreshData();
        if(mPresenter==null)
            return;
        isFresh = true;
        loadData();
    }

    private void initView() {
        //查蛋页面默认查的是今天往前推7天
        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerview.addItemDecoration(new CustomGridItemDecoration(1));
        adapter = new CheckKWAdapter(R.layout.adapter_check_kw_list_item, data);
        recyclerview.setAdapter(adapter);
        adapter.setiCageInterface((pos, c_id, medhid) -> {
            updateListSelected(pos, c_id, medhid);
        });

        xRefreshView.setHideFooterWhenComplete(true);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                isFresh = true;
                loadData();

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                isFresh = false;
                loadData();
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            // loadData(true);
        }
    }

    //跳转补录下蛋日期
    private void goAddLayEggDate(CageBeanVo bean) {
        Intent go = new Intent();
        go.setClass(getActivity(), AddLayEggDateProduceActivity.class);
        if (bean != null) {
            go.putExtra(Constant.INTENT_C_ID, bean.getId());
            go.putExtra(Constant.INTENT_C_NUMBER, bean.getNumber());
            go.putExtra(Constant.INTENT_WH_ID, MainActivity.wh_id);
            go.putExtra(Constant.INTENT_WH_NUMBER, MainActivity.wh_number);
        }
        startActivity(go);
    }

    private void saveEgg(int pos, boolean isBadEgg, boolean isEmpty) {
        this.isEmpty = isEmpty;
        selectPos = pos;
        CageBeanVo model = data.get(pos);
        FertilizeParam param = new FertilizeParam();
        int egg_status = DBSqlUtil.getFerilizeStatusByTpye(0);
        param.setWh_id(MainActivity.wh_id);
        param.setNumber(model.getNumber());
        param.setC_id(model.getId());
        param.setFudan(!isEmpty);
        param.setBadEgg(isBadEgg);
        param.setEmptyCage(isEmpty);
        param.setEgg_status(egg_status);
        param.setLay_egg_date("");
        operatePresenter.saveLayEgg(param);

//        boolean isResult = DBSqlUtil.saveLayEgg(MainActivity.wh_id, c_number, isEmpty, isBadEgg);
//        if (isResult) {
//            MyToast.showLong("操作成功");
//            data.remove(pos);
//            adapter.notifyDataSetChanged();
//        }
    }

    //更新列表选择状态
    public void updateListSelected(int pos, int c_id, int medhid) {
        switch (medhid) {
            case 1://确认
                DialogHelper.showLayEggDialog(getActivity(), (isBadEgg, isEmpty) -> {

                    saveEgg(pos, isBadEgg, isEmpty);
                });
                break;
            case 2://补录下蛋日期
                goAddLayEggDate(data.get(pos));
                break;
        }

    }


    private void loadData() {
        cageParam.setWh_id(MainActivity.wh_id);
        if (isFresh) {
            cageParam.setPageNum(1);
        } else {
            cageParam.setPageNum(cageParam.getPageNum() + 1);
        }
        mPresenter.getKWListByPage(cageParam);

    }

    @Override
    public void onAddCageSuccess(Integer bean) {

    }

    @Override
    public void onQueryCageSuccess(PageInfo<CageBeanVo> result) {
        if (isFresh) {
            data.clear();
        }
        if (result == null || result.getList() == null) {
            if (isFresh) {
                xRefreshView.stopRefresh();
            } else {
                xRefreshView.stopLoadMore();
            }
            return;
        }
        List<CageBeanVo> list = result.getList();
        data.addAll(list);

        if (!isFresh && !result.isHasNextPage()) {
            MyToast.showLong("没有更多数据了");
        }
        LogUtil.e("TAG", list.size() + "");
        if (!result.isHasNextPage()) {
            xRefreshView.setPullLoadEnable(false);
        } else {
            xRefreshView.setPullLoadEnable(true);
        }
        if (isFresh) {
            xRefreshView.stopRefresh();
        } else {
            xRefreshView.stopLoadMore();

        }
        adapter.notifyDataSetChanged();


    }

    @Override
    public void onUpdateCageSuccess(int c_id) {

    }

    @Override
    public void onFailure(String error) {

    }

    @Override
    public void onCheckEggSuccess(PageInfo<FertilizeBean> list) {

    }

    @Override
    public void onQueryCateStatusRecordSuccess(List<CageStatusRecordBean> list) {

    }

    @Override
    public void onSaveLayEggSuccess(int c_id) {
        MyToast.showLong("操作成功");
        if (selectPos >= 0 && !isEmpty) {
            data.remove(selectPos);
            adapter.notifyDataSetChanged();
            isEmpty = true;
            selectPos = -1;
        }


    }

    @Override
    public void onAddOperate(int type) {

    }

    @Override
    public void onFailed(String error) {

    }
}
